package com.example.charlotte.snakegame;

import android.content.Context;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteOpenHelper;



public class MyDB {
    public class MySQLiteOpenHelper extends SQLiteOpenHelper {
        private String createTable = "create table user_tb(_id integer primary key autoincrement,username text,password text,score int,title text)";

        public MySQLiteOpenHelper(Context context, String name,
                                  SQLiteDatabase.CursorFactory factory, int version) {
            super(context, name, factory, version);
        }

        @Override
        public void onCreate(SQLiteDatabase sqLiteDatabase) {
            sqLiteDatabase.execSQL(createTable);
        }

        @Override
        public void onUpgrade(SQLiteDatabase sqLiteDatabase, int oldVersion, int newVersion) {
            switch (oldVersion) {
                case 1:
                    //执行升级语句 sqLiteDatabase.execSQL(updateTable1);
                case 2:
                    //执行升级语句 sqLiteDatabase.execSQL(updateTable2);
                    break;
            }
        }
    }

    private Context context;
    private String name;
    private SQLiteDatabase sqLiteDatabase;

    public MyDB(Context context, String name) {
        this.context = context;
        this.name = name;
    }

    public void open() {
        MySQLiteOpenHelper mySQLiteOpenHelper = new MySQLiteOpenHelper(context, name, null, 1);
        sqLiteDatabase = mySQLiteOpenHelper.getReadableDatabase();
    }

    public void close() {
        if (sqLiteDatabase != null) {
            sqLiteDatabase.close();
        }
    }
    public void insert(User user) {
        sqLiteDatabase.execSQL("insert into user_tb values(null,?,?,?,?)", new Object[]{user.getUserName(), user.getPassword(),user.getScore(),user.getTitle()});
    }


    public Cursor queryByName(String userName, String password) {
        return sqLiteDatabase.rawQuery("select * from user_tb where username=? and password=?", new String[]{userName, password});

    }
    public Cursor queryName(String userName) {
        return sqLiteDatabase.rawQuery("select * from user_tb where username=?", new String[]{userName});
    }


}
